package soar.test.thread.notify;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Receiver extends Thread
{
    private final static Logger logger = LoggerFactory
            .getLogger(Receiver.class);

    private ArrayList<Request> requests = new ArrayList<>();

    public Receiver() throws Exception
    {
    }

    public synchronized void put(Request request)
    {
        requests.add(request);
        notify();
    }

    public void run()
    {
        while (true)
        {
            ArrayList<Request> reqts;
            synchronized (this)
            {
                while (this.requests.isEmpty())
                {
                    try
                    {
                        wait();
                        // logger.info("wait finish {}", count);
                    }
                    catch (Exception e)
                    {
                        logger.info("{}", e);
                    }
                }
                reqts = this.requests;
                this.requests = new ArrayList<>();
            }


            int count=0;
            for (Request reqt : reqts)
            {
                logger.info("id({})", reqt.id);
                ++count;
            }
            logger.info("reqts process {}", count);
        }
    }
}
